<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="分类名称" prop="name">
        <el-input class="w500" v-model="form.name" placeholder="默认不超过14字" :maxlength="14"></el-input>
      </el-form-item>
      <el-form-item label="分类描述" prop="description">
        <el-input class="w500" v-model="form.desc" placeholder="例如：描述"></el-input>
      </el-form-item>
      <el-form-item label="分类标识" prop="action">
        <el-input class="w500" v-model="form.action" placeholder="biaoshi"></el-input>
      </el-form-item>
      <el-form-item label="添加图片">
        <el-upload
          :action="upload_url"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-success="handleAvatarSuccess">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog v-model="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="是否公开">
        <el-radio-group v-model="form.is_public">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序">
        <el-input class="w500" v-model="form.order" type="number"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import notify from '@/tools/notify'
  import { UPLOAD_URL } from '@/config/const'

  export default {
    data: function () {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        form: {
          name: '',
          app_id: '',
          other_app_read: '',
          is_public: 0,
          action: '',
          desc: '',
          order: 1,
          pid: 0,
          profile_pic: ''
        },
        rules: {
          name: [
            { required: true, message: '请输入分类名称', trigger: 'blur' }
          ],
          action: [
            { required: true, message: '请输入分类标识', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      ...mapState({
        saveData: state => state.recommend.saveData,
        cover: state => state.recommend.cover
      }),
      upload_url () {
        return UPLOAD_URL
      }
    },
    methods: {
      onSubmit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.form.pid = this.saveData.id
            this.form.app_id = this.saveData.app_id
            this.form.other_app_read = this.saveData.other_app_read
            this.form.profile_pic = 'http://' + this.cover.save_path + '/' + this.cover.filename
            this.$store.dispatch('recommend/recommendAdd', this.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/goods/recommend')
            })
          }
        })
      },
      handleAvatarSuccess (file, response, fileList) {
        const self = this
        if (file.code !== 200) {
          fileList.pop()
          self.$message({
            message: '封面上传失败',
            type: 'error'
          })
        } else {
          self.$message({
            message: '封面上传成功',
            type: 'success'
          })
        }
        self.$store.commit('recommend/RECOMMEND_COVER', file.res)
      },
      handlePictureCardPreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
